package com.example.tt_bells_v02;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wheel.WheelView;

/*
 * Class extends WheelView to vertical LinearLayout contained
 * WheelView with TextView caption and managed margins.
 * 
 * Caption can be setted up from xml or by calling setCaption.
 * + other params from xml
 */

public class WheelViewCaptioned extends LinearLayout {

	public String 			caption;
	public final TextView 	cap;
	public final WheelView 	wheel;
	
	final float dp_scale = getResources().getDisplayMetrics().density;
	
	public int dpToPx(int dp) {
		return (int)(dp*dp_scale+0.5f);
	}
	
	public WheelViewCaptioned(Context context, AttributeSet attrs) {
		super(context, attrs);

		setOrientation(VERTICAL);
		
		cap = new TextView(context);
		LayoutParams par_cap = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,1);
		addView(cap,par_cap);
		
		wheel = new WheelView(context);
		LayoutParams par_wheel = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,1);
		par_wheel.setMargins(dpToPx(3), dpToPx(2), dpToPx(3), dpToPx(2));
		addView(wheel, par_wheel);
		
		if (attrs != null) {
			TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.WheelViewCaptioned);
			final int N = a.getIndexCount();
			for (int i = 0; i < N; ++i) {
				int attr = a.getIndex(i);
			    switch (attr) {
			        case R.styleable.WheelViewCaptioned_cyclic:
			            wheel.setCyclic(a.getBoolean(attr, false));
			            break;
			        case R.styleable.WheelViewCaptioned_visible_items:
			            wheel.setVisibleItems(a.getInt(attr, 3));
			            break;
			    }

			}
			a.recycle();
			
			caption = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "text");
			if (caption != null) cap.setText(caption);
		}
	}

	public void setCaption(CharSequence text) {
		if (text != null) cap.setText(text);
	}
	
	//TODO: maybe setters for wheel parameters
}
